package com.fy.auth.controller;

import com.fy.auth.feign.PaymentFeignService;
import com.fy.auth.feign.UserFeignService;
import com.fy.auth.vo.UserLoginVo;
import com.fy.auth.vo.UserRegisterVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @author 芣苡
 */
@RestController
public class LoginController {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private PaymentFeignService paymentFeignService;
    @Autowired
    private UserFeignService userFeignService;

    /**
     * 功能描述:获取短信验证码
     *
     * @return : java.lang.String
     * @author : fuyi
     * @date : 2022-06-09 16:31
     */
    @GetMapping(value = "/sms/sendCode")
    public String sendCode(@RequestParam("phone") String phone) {
        //1、接口防刷，判断redis中是否存在，存在就是重复
        String redisCode = stringRedisTemplate.opsForValue().get("sms:code:" + phone);
        if (!StringUtils.isEmpty(redisCode)) {
            //活动存入redis的时间，用当前时间减去存入redis的时间，判断用户手机号是否在60s内发送验证码
            long currentTime = Long.parseLong(redisCode.split("_")[1]);
            if (System.currentTimeMillis() - currentTime < 60000) {
                //60s内不能再发
                return "访问次数平凡";
            }
        }
        //生成验证码
        int code = (int) ((Math.random() * 9 + 1) * 100000);
        String codeNum = String.valueOf(code);

        String redisStorage = codeNum + "_" + System.currentTimeMillis();
        //存入redis，防止同一个手机号在60秒内再次发送验证码
        stringRedisTemplate.opsForValue().set("sms:code:" + phone, redisStorage, 10, TimeUnit.MINUTES);
        paymentFeignService.sendCode(phone, codeNum);
        return null;
    }

    /**
     * 功能描述: 注册账号
     * 〈〉
     *
     * @return : java.lang.String
     * @author : fuyi
     * @date : 2022-06-10 14:14
     */
    @PostMapping(value = "/register")
    public String register(UserRegisterVo vos) {
        //1、效验验证码
        String userCode = vos.getCode();
        //获取存入Redis里的验证码
        String redisCode = stringRedisTemplate.opsForValue().get("sms:code:" + vos.getPhone());
        if (StringUtils.isEmpty(redisCode)) {
            return "验证已过期";
        }
        //判断redis的验证码和接收的验证码是否匹配
        if (!userCode.equals(redisCode.split("_")[0])) {
            return "输入的验证码错误";
        }
        //删除验证码;令牌机制
        stringRedisTemplate.delete("sms:code:" + vos.getPhone());
        //验证码通过，真正注册，调用远程服务进行注册
        Boolean register = userFeignService.register(vos);
        if (register) {
            //成功
            return "注册成功";
        } else {
            //失败
            return "注册失败";
        }
    }

    @PostMapping(value = "/login")
    public String login(UserLoginVo vo) {
        String login = userFeignService.login(vo);
        //判断登录是否成功
        return login;
    }
}
